﻿using EquipmentManager.BOLogic;
using EquipmentManager.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EquipmentManager.Models;

namespace EquipmentManager.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        [Authorize]
        public ActionResult Index()
        {
            UserBO usersBO = new UserBO();
            User curUser = usersBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                MainPageModel mainPageModel = new MainPageModel();
                EquipmentBO equipmentBO = new EquipmentBO();

                mainPageModel.Equipments = equipmentBO.GetAllRecentEquipments();
                mainPageModel.Users = usersBO.GetRecentUserList();

                return View(mainPageModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        public ActionResult ProfileChange()
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                return RedirectToAction("EditUser", "Admin", new { UserId = curUser.Id });
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        public ActionResult ExportAllEquipments()
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();
            EquipmentBO equipmqntBO = new EquipmentBO();

            if (curUser.Role == UserRole.Admin)
            {
                return View("~/Views/Shared/ExportAllEquipments.cshtml", equipmqntBO.GetAllEquipments());
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        public ActionResult Users()
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                var users = userBO.GetUserList();

                return View(users);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpGet]
        public ActionResult CreateUser(long RegionId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();
                CreateModel<User> model = new CreateModel<User>();
                model.ParentId = RegionId;
                model.Instance.Region = regionBO.GetRegion(RegionId);

                return View(model);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult CreateUser(CreateModel<User> userModel)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();

                userModel.Instance.Region = regionBO.GetRegion(userModel.ParentId);

                if (ModelState.IsValid)
                {
                    if (userBO.IsLoginUnique(userModel))
                    {
                        if (userBO.CreateNewUser(userModel))
                        {
                            //используем ВьюБег для возврата к предыдущей странице
                            return RedirectToAction("PostOffices", "Admin", new { RegionId = userModel.ParentId });
                        }
                        else
                        {
                            ModelState.AddModelError("", "Правильно заполните поля!");
                            return View(userModel);
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Пользователь с таким логином уже существует!");
                        return View(userModel);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(userModel);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult EditUser(long UserId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                CreateModel<User> userModel = new CreateModel<User>();
                userModel.Instance = userBO.GetUser(UserId);
                userModel.Instance.Password = String.Empty;
                userModel.ParentId = userModel.Instance.Region.Id;

                return View(userModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost]
        [Authorize]
        public ActionResult EditUser(CreateModel<User> userModel)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();

                userModel.Instance.Region = regionBO.GetRegion(userModel.ParentId);

                if (ModelState.IsValid)
                {
                    if (userBO.EditUser(userModel))
                    {
                        return RedirectToAction("PostOffices", "Admin", new { RegionId = userModel.ParentId });
                    }

                    return View(userModel);
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(userModel);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult DeleteUser(long UserId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                CreateModel<User> userModel = new CreateModel<User>();
                userModel.Instance = userBO.GetUser(UserId);
                userModel.ParentId = userModel.Instance.Region.Id;

                return View(userModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost, ActionName("DeleteUser")]
        [Authorize]
        public ActionResult DeleteUserConfirmed(CreateModel<User> userModel, long UserId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {

                if (userBO.DeleteUser(UserId))
                {
                    return RedirectToAction("PostOffices", "Admin", new { RegionId = userModel.ParentId });
                }

                return View(UserId);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        public ActionResult Directorates()
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                DirectorateBO directorateBO = new DirectorateBO();
                var directorates = directorateBO.GetDirectorateList();

                return View(directorates);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpGet]
        public ActionResult CreateDirectorate()
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                Directorate model = new Directorate();

                return View(model);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult CreateDirectorate(Directorate model)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                DirectorateBO directorateBO = new DirectorateBO();

                if (ModelState.IsValid)
                {
                    if (directorateBO.CreateNewDirectorate(model))
                    {
                        //используем ВьюБег для возврата к предыдущей странице
                        return RedirectToAction("Directorates", "Admin");
                    }
                    else
                    {
                        return View(model);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(model);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult EditDirectorate(long DirectorateId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                DirectorateBO directorateBO = new DirectorateBO();

                return View(directorateBO.GetDirectorate(DirectorateId));
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost]
        [Authorize]
        public ActionResult EditDirectorate(Directorate directorateModel)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                DirectorateBO directorateBO = new DirectorateBO();

                if (ModelState.IsValid)
                {
                    if (directorateBO.EditDirectorate(directorateModel))
                    {
                        return RedirectToAction("Directorates", "Admin");
                    }

                    return View(directorateModel);
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(directorateModel);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult DeleteDirectorate(long DirectorateId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                DirectorateBO directorateBO = new DirectorateBO();

                return View(directorateBO.GetDirectorate(DirectorateId));
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost, ActionName("DeleteDirectorate")]
        [Authorize]
        public ActionResult DeleteDirectorateConfirmed(long DirectorateId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                DirectorateBO directorateBO = new DirectorateBO();

                if (directorateBO.DeleteDirectorate(DirectorateId))
                {
                    return RedirectToAction("Directorates", "Admin");
                }

                return View(DirectorateId);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        public ActionResult Regions(long DirectorateId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();
                DirectorateBO directorateBO = new DirectorateBO();

                ListModel<Region, Directorate> model = new ListModel<Region, Directorate>();
                model.Parent = directorateBO.GetDirectorate(DirectorateId);
                model.ListItems = regionBO.GetRegions(DirectorateId);

                return View(model);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpGet]
        public ActionResult CreateRegion(long DirectorateId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                DirectorateBO directorateBO = new DirectorateBO();

                CreateModel<Region> model = new CreateModel<Region>();
                model.ParentId = DirectorateId;
                model.Instance.Directorate = directorateBO.GetDirectorate(DirectorateId);

                return View(model);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult CreateRegion(CreateModel<Region> regionModel)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();
                DirectorateBO directorateBO = new DirectorateBO();

                regionModel.Instance.Directorate = directorateBO.GetDirectorate(regionModel.ParentId);

                if (ModelState.IsValid)
                {
                    if (regionBO.CreateNewRegion(regionModel))
                    {
                        //используем ВьюБег для возврата к предыдущей странице
                        return RedirectToAction("Regions", "Admin", new { DirectorateId = regionModel.ParentId });
                    }
                    else
                    {
                        return View(regionModel);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(regionModel);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult EditRegion(long RegionId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();

                CreateModel<Region> regionModel = new CreateModel<Region>();
                regionModel.Instance = regionBO.GetRegion(RegionId);
                regionModel.ParentId = regionModel.Instance.Directorate.Id;

                return View(regionModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost]
        [Authorize]
        public ActionResult EditRegion(CreateModel<Region> regionModel)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();
                DirectorateBO directorateBO = new DirectorateBO();

                regionModel.Instance.Directorate = directorateBO.GetDirectorate(regionModel.ParentId);

                if (ModelState.IsValid)
                {

                    if (regionBO.EditRegion(regionModel))
                    {
                        return RedirectToAction("Regions", "Admin", new { DirectorateId = regionModel.ParentId });
                    }

                    return View(regionModel);
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(regionModel);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult DeleteRegion(long RegionId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();

                CreateModel<Region> regionModel = new CreateModel<Region>();
                regionModel.Instance = regionBO.GetRegion(RegionId);
                regionModel.ParentId = regionModel.Instance.Directorate.Id;

                return View(regionModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost, ActionName("DeleteRegion")]
        [Authorize]
        public ActionResult DeleteRegionConfirmed(CreateModel<Region> regionModel, long RegionId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();

                if (regionBO.DeleteRegion(RegionId))
                {
                    return RedirectToAction("Regions", "Admin", new { DirectorateId = regionModel.ParentId });
                }

                return View(RegionId);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        public ActionResult PostOffices(long RegionId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                PostOfficeBO postOfficeBO = new PostOfficeBO();
                RegionBO regionBO = new RegionBO();

                ListModel<PostOffice, Region> model = new ListModel<PostOffice, Region>();
                model.Parent = regionBO.GetRegion(RegionId);
                model.ListItems = postOfficeBO.GetPostOffices(RegionId);

                return View(model);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpGet]
        public ActionResult CreatePostOffice(long RegionId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                RegionBO regionBO = new RegionBO();

                CreateModel<PostOffice> model = new CreateModel<PostOffice>();
                model.ParentId = RegionId;
                model.Instance.Region = regionBO.GetRegion(RegionId);

                return View(model);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult CreatePostOffice(CreateModel<PostOffice> postOfficeModel)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                PostOfficeBO postOfficeBO = new PostOfficeBO();
                RegionBO regionBO = new RegionBO();

                postOfficeModel.Instance.Region = regionBO.GetRegion(postOfficeModel.ParentId);

                if (ModelState.IsValid)
                {
                    if (postOfficeBO.CreateNewPostOffice(postOfficeModel))
                    {
                        //используем ВьюБег для возврата к предыдущей странице
                        return RedirectToAction("PostOffices", "Admin", new { RegionId = postOfficeModel.ParentId });
                    }
                    else
                    {
                        return View(postOfficeModel);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(postOfficeModel);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult EditPostOffice(long PostOfficeId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                PostOfficeBO postOfficeBO = new PostOfficeBO();

                CreateModel<PostOffice> postOffice = new CreateModel<PostOffice>();
                postOffice.Instance = postOfficeBO.GetPostOffice(PostOfficeId);
                postOffice.ParentId = postOffice.Instance.Region.Id;

                return View(postOffice);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost]
        [Authorize]
        public ActionResult EditPostOffice(CreateModel<PostOffice> postOfficeModel)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                PostOfficeBO postOfficeBO = new PostOfficeBO();
                RegionBO regionBO = new RegionBO();

                postOfficeModel.Instance.Region = regionBO.GetRegion(postOfficeModel.ParentId);

                if (ModelState.IsValid)
                {

                    if (postOfficeBO.EditPostOffice(postOfficeModel))
                    {
                        return RedirectToAction("PostOffices", "Admin", new { RegionId = postOfficeModel.ParentId });
                    }

                    return View(postOfficeModel);
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(postOfficeModel);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult DeletePostOffice(long PostOfficeId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                PostOfficeBO postOfficeBO = new PostOfficeBO();

                CreateModel<PostOffice> postOfficeModel = new CreateModel<PostOffice>();
                postOfficeModel.Instance = postOfficeBO.GetPostOffice(PostOfficeId);
                postOfficeModel.ParentId = postOfficeModel.Instance.Region.Id;

                return View(postOfficeModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost, ActionName("DeletePostOffice")]
        [Authorize]
        public ActionResult DeletePostOfficeConfirmed(CreateModel<PostOffice> postOfficeModel, long PostOfficeId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                PostOfficeBO postOfficeBO = new PostOfficeBO();

                if (postOfficeBO.DeletePostOffice(PostOfficeId))
                {
                    return RedirectToAction("PostOffices", "Admin", new { RegionId = postOfficeModel.ParentId });
                }

                return View(PostOfficeId);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        public ActionResult AllEquipments()
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                EquipmentBO equipmentBO = new EquipmentBO();
                SearchModel searchModel = new SearchModel();

                return View(searchModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        public ActionResult SearchEquipmentResult(string searchText)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                EquipmentBO equipmentBO = new EquipmentBO();

                List<EquipmentBase> equipmentModel = new List<EquipmentBase>();
                equipmentModel = equipmentBO.SearchEquipments(searchText);

                return View("_SearchEquipmentResultAdmin", equipmentModel);
            }
            else
            {
                return View("_SearchEquipmentResultAdmin", new List<EquipmentBase>());
            }
        }

        [Authorize]
        public ActionResult Equipments(long PostOfficeId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                PostOfficeBO postOfficeBO = new PostOfficeBO();
                EquipmentBO equipmentBO = new EquipmentBO();

                ListModel<EquipmentBase, PostOffice> equipmentModel = new ListModel<EquipmentBase, PostOffice>();
                equipmentModel.Parent = postOfficeBO.GetPostOffice(PostOfficeId);
                equipmentModel.ListItems = equipmentBO.GetEquipments(PostOfficeId);

                return View(equipmentModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        public ActionResult DetailEquipment(long EquipmentId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                EquipmentBO equipmentBO = new EquipmentBO();

                CreateModel<EquipmentBase> equipmentModel = new CreateModel<EquipmentBase>();
                equipmentModel.Instance = equipmentBO.GetEquipment(EquipmentId);
                equipmentModel.ParentId = equipmentModel.Instance.PostOffice.Id;

                return View(equipmentModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpGet]
        public ActionResult CreateEquipment(long PostOfficeId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                PostOfficeBO postOfficeBO = new PostOfficeBO();

                CreateModel<EquipmentBase> equipmentModel = new CreateModel<EquipmentBase>();
                equipmentModel.ParentId = PostOfficeId;
                equipmentModel.Instance.PostOffice = postOfficeBO.GetPostOffice(PostOfficeId);
                equipmentModel.Instance.State = State.Good;

                return View(equipmentModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult CreateEquipment(CreateModel<EquipmentBase> equipmentModel)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                int checkNumb;

                EquipmentBO equipmentBO = new EquipmentBO();
                PostOfficeBO postOfficeBO = new PostOfficeBO();

                equipmentModel.Instance.PostOffice = postOfficeBO.GetPostOffice(equipmentModel.ParentId);

                if (ModelState.IsValid && (int.TryParse(equipmentModel.Instance.Number, out checkNumb) || equipmentModel.Instance.Number == "б/н"))
                {
                    if (equipmentBO.CreateNewEquipment(equipmentModel))
                    {
                        //используем ВьюБег для возврата к предыдущей странице
                        return RedirectToAction("Equipments", "Admin", new { PostOfficeId = equipmentModel.ParentId });
                    }
                    else
                    {
                        ModelState.AddModelError("", "Правильно заполните поля!");
                        return View(equipmentModel);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(equipmentModel);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult EditEquipment(long EquipmentId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                EquipmentBO equipmentBO = new EquipmentBO();

                CreateModel<EquipmentBase> equipmentModel = new CreateModel<EquipmentBase>();
                equipmentModel.Instance = equipmentBO.GetEquipment(EquipmentId);
                equipmentModel.ParentId = equipmentModel.Instance.PostOffice.Id;

                return View(equipmentModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost]
        [Authorize]
        public ActionResult EditEquipment(CreateModel<EquipmentBase> equipmentModel)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                EquipmentBO equipmentBO = new EquipmentBO();
                PostOfficeBO postOfficeBO = new PostOfficeBO();

                equipmentModel.Instance.PostOffice = postOfficeBO.GetPostOffice(equipmentModel.ParentId);

                if (ModelState.IsValid)
                {
                    if (equipmentBO.EditEquipment(equipmentModel))
                    {
                        return RedirectToAction("Equipments", "Admin", new { PostOfficeId = equipmentModel.ParentId });
                    }

                    return View(equipmentModel);
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(equipmentModel);
                }
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult DeleteEquipment(long EquipmentId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                EquipmentBO equipmentBO = new EquipmentBO();

                CreateModel<EquipmentBase> equipmentModel = new CreateModel<EquipmentBase>();
                equipmentModel.Instance = equipmentBO.GetEquipment(EquipmentId);
                equipmentModel.ParentId = equipmentModel.Instance.PostOffice.Id;

                return View(equipmentModel);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }

        [HttpPost, ActionName("DeleteEquipment")]
        [Authorize]
        public ActionResult DeleteEquipmentConfirmed(CreateModel<EquipmentBase> equipmentModel, long EquipmentId)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            if (curUser.Role == UserRole.Admin)
            {
                EquipmentBO equipmentBO = new EquipmentBO();

                if (equipmentBO.DeleteEquipment(EquipmentId))
                {
                    return RedirectToAction("Equipments", "Admin", new { PostOfficeId = equipmentModel.ParentId });
                }

                return View(EquipmentId);
            }
            else
            {
                return RedirectToAction("Unathorized", "Error");
            }
        }
    }
}
